// pages/MyMessage/MyMessage.js
var _mTab = [{ id: 0, text: '软件开发' }, { id: 1, text: 'AI' }, { id: 2, text: '区块链' }]
var _tabs = [{ id: 0, text: '软件开发', isMine: false }, { id: 1, text: 'AI', isMine: false }, { id: 3, text: '区块链2', isMine: false }, { id: 2, text: '区块链', isMine: false}, { id: 4, text: '区块链3', isMine: false }, { id: 5, text: '区块链4', isMine: false }]
const axios = require('../../utils/http.js');
const utils = require('../../utils/util.js');
const baseFn = utils.baseFn;
const formatTime = utils.formatTime
const app = getApp()
Page({

  /**
   * 页面的初始数据 , 'AI', '大数据', 'APP', '微信', 'AI', '大数据', 'APP', '微信'
   */

  data: {
    user_id:app.globalData.userInfo.user_id,
    comName:'',
    comPos:'',
    mTab: _mTab,
    texts: ['软件开发', 'AI', '大数据', 'APP', 'I', '大数据', 'APP','其他'],
    index: 0,
    area:['北京','上海','广州','其他'],
    areaIds:0,
    tabs: [],
    tabindex:0,
    userInfo: app.globalData.userInfo,
    mLabel:[],
    hiddenmodalput:true,
    modalAlert:'',
    newLabel:''
  },
  bindPickerAreaChange:function(e){
    //console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      areaIds: e.detail.value,
      ['userInfo.user_area']: this.data.area[e.detail.value]
    })
  },
  bindPickerChange: function (e) {
    //console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value,
      ['userInfo.calling']: this.data.texts[e.detail.value]
    })
  },
  comPosInputChange: function (e) {
    this.setData({
      ['userInfo.user_title']: e.detail.value
    })
  },
  comNameInputChange: function (e) {
    this.setData({
      ['userInfo.user_compony']: e.detail.value
    })
  },
  bindDeltab: function(e){
    /*
      var _es = e.currentTarget.dataset.tabid
      var _fil = this.data.mTab.filter((val)=>{
        return val.id != _es
      })
      this.setData({
        mTab: _fil
      })
      var _id = e.currentTarget.dataset.tabid;
      for (var i = 0; i < _tabs.length; i++) {
        if (_tabs[i].id == _id) {
          _tabs[i].isMine = false
        }
      }
    */
    var _es = e.currentTarget.dataset.idx
    this.tabArrs(2, _es)
  },
  bindAddtab:function(){
    var query = wx.createSelectorQuery()
    console.log(query.select('.tabpick'))
    query.select('.tabpick').clicks()
  },
  bindAddLabel: function () {
    this.setData({ hiddenmodalput:false})
  },
  modalcancel:function(){
    this.setData({
      hiddenmodalput:true,
      modalAlert:'',
      newLabel: ''
    })
  },
  modalconfirm:function(){
    var _newLabel = this.data.newLabel
    if (baseFn.notNull(_newLabel)){
      var _hasexit = this.data.mLabel.some(function(val){
        return val == _newLabel
      })
      if (_hasexit){
        this.setData({
          modalAlert: '这个标签已经存在喽'
        })
        return false
      }
    }else{
      this.setData({
        modalAlert: '请填写标签名'
      })
      return false
    }
    this.setData({
      hiddenmodalput: true,
      modalAlert: '',
      newLabel:'',
    })
    this.tabArrs(1, _newLabel)
  },
  inputLabelChange:function(e){
    this.setData({
      newLabel: e.detail.value,
      modalAlert: ''
    })
  },
  bindPickerTabChange:function(e){
    var _selIds = e.detail.value;
    var _curMTabs = this.data.mTab, _curTabs = this.data.tabs;
    var _id = _curTabs[_selIds].id;
    for (var i = 0; i < _tabs.length;i++ ){
      if (_tabs[i].id == _id){
        _tabs[i].isMine = true
      }
    }
    this.tabArrs(1,'')
  },
  /**
   * 生命周期函数--监听页面加载
   */
  tabArrs:function(type=1,idx){
    var _curTabels = this.data.mLabel 
    if(type==1){
      //增
      if (baseFn.notNull(idx)){
        _curTabels.push(idx)
      }
      
    }else if(type == 2){
      //减
      _curTabels.splice(idx, 1)
    }
    
    this.setData({
      mLabel: _curTabels,
      ['userInfo.user_lable']: _curTabels.join(',')
    })
    // console.log(this.data)
    // var _mTabs = _tabs.filter(function (val) {
    //   return val.isMine 
    // });
    // var _nTabs = _tabs.filter(function (val) {
    //   return !val.isMine
    // });
    // this.setData({ tabs: _nTabs, mTab: _mTabs})
  },
  editProjectClick: function () {
    var _url = axios.urlR.userRes, isEdit = baseFn.notNull(this.data.userInfo.user_id)
    var _userData = {
      user_nickname: this.data.userInfo.user_nickname,
      user_phone: '',
      user_compony: this.data.userInfo.user_compony,
      user_img: this.data.userInfo.user_img,
      calling: this.data.userInfo.calling,
      user_area: this.data.userInfo.user_area,
      user_lable: this.data.userInfo.user_lable,
      user_title: this.data.userInfo.user_title
    }
    if (isEdit) {
      _url = axios.urlR.editUserInfo
      _userData.user_id = this.data.userInfo.user_id
    } else {
      _userData.user_nickname = app.globalData.wxUserInfo.nickName
      _userData.user_img = app.globalData.wxUserInfo.avatarUrl,
      _userData.openid = app.globalData.openid
    }
    axios.POST(true, _url, _userData, (res) => {
      if (isEdit) {
        axios.model('成功','更新成功',false,undefined,undefined,function(){
          wx.switchTab({
            url: '../nMine/nMine',
          })
        })
      } else {
        app.globalData.userInfo.user_id = res.user_id
        // console.log(app.globalData)
        axios.model('成功', '绑定成功', false, undefined, undefined, function () {
          
          
          wx.switchTab({
            url: '../nMine/nMine'
          })
        })
        
      }
    }, () => { })

  },
  onLoad: function (options) {
    this.setData({
      ['userInfo.user_id']: app.globalData.userInfo.user_id
    })
    if (!baseFn.notNull(this.data.userInfo.user_id)) {
      wx.setNavigationBarTitle({
        title: '新注册'
      })
      this.setData({
        openid: app.globalData.openId,
        ['userInfo.calling']: this.data.texts[0],
        ['userInfo.user_area']: this.data.area[0]
      })
    }else{
      axios.POST(true, axios.urlR.userInfo, { user_id: app.globalData.userInfo.user_id}, (res) => {
        var _areaId = 0, _textsId=0
        //区域初始化
        for (var i = 0; i < this.data.area.length;i++){
          if (this.data.area[i] == res.user_area){
            _areaId = i
            break;
          }
        }
        //行业初始化
        for (var j = 0; j < this.data.texts.length; j++) {
          if (this.data.texts[j] == res.user_area) {
            _textsId = j
            break;
          }
        }
        this.setData({ areaIds: _areaId,index: _textsId,userInfo: res, mLabel:res.user_lable.split(',')})
        this.setData({ ['userInfo.user_id']:app.globalData.userInfo.user_id})
      }, () => { })
    }
    /*
      for (var i = 0; i < _tabs.length;i++){
        var _tabsId = _tabs[i].id;
        var isChange=_mTab.some((val)=>{
          return val.id == _tabsId
        })
        if (isChange){
          _tabs[i].isMine = true
        }else{
          _tabs[i].isMine = false
        }
      }
      this.tabArrs()
    */
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})